
$.fn.BackgroundChange = function(defaultImage, fadeoutcallback, fadeincallback)
{            
    
    $('.content').append('<div class="image-preloader"><p>Trwa ładowanie fotografi</p></div>');
    w = $('div[class="image-preloader"]').width();
    h = $('div[class="image-preloader"]').height();
    $('div[class="image-preloader"]').css({
        left: ($('.content').width()-w)/2,
        top: '50px',
        'display':'block'});

    
    image = (new String(defaultImage!=null?defaultImage:"image/"+$(this).attr('hash')))+'?rand='+(new Date()).getTime();
    $('body').remove('.background-cache');
    $('body').append('<img src="'+image+'" class="background-cache"/>');

    $('img[class=background-cache]').load(function()
    {                            
        $('.image-preloader').remove();
        $('.background2').css('background-image','none');
        if($('img[class=background]').attr('src')!='')
        {
            $('img[class=background]').fadeOut(1500,function(){
                $('.background2').css('background-image','none');
                $('img[class=background]').css('display','none');
                if(typeof(fadeoutcallback) == "function")
                {                   
                        fadeoutcallback();
                }
                
                $(this).attr('src',image).delay(500).fadeIn(2000,function(){
                    if(typeof(fadeincallback) == "function")
                    {
                        fadeincallback();
                    }
                });           
            });
        }else{
            
            $('img[class=background]').css('display','none').attr('src',image).fadeIn(2000,function(){
                $('.background2').css('background-image','none');
                 if(typeof(fadeincallback) == "function")
                    {
                        fadeincallback();
                    }
            });           
        }
        $(this).remove();     
     });
}

$.fn.ActiveGallery = function()
{
    
   
    $(document).on('click','div[id=ImageButton]',function(){        
        $('div[id=ImageButton]').each(function(){
            $(this).attr('active',0);        
        });
        
        $(this).BackgroundChange();   
        $(this).attr('active',1);                 
    });   

 $('div[id=ImageButton]:first').click();     
}

$.fn.Stop = function(images)
{
    Images = new Array();
}

$.fn.LoopedGallery = function(images)
{
    if($(this).attr('looped') == 'false') return;    
    
    var id = $(this).attr('image_id');
    if(!id)
    {        
        id = 0;
    }
    
    var _this = $(this);
    var image = images[id];
     
    $('.background').BackgroundChange(image,function(){
        if($(this).attr('looped') == 'false')
        {
            return false;    
        }
    },function(){           
        if($(this).attr('looped') == 'false')
        {
            return false;    
        }
        id++;
        if(id+1 > images.length)
        {
            id = 0;
        }
        _this.attr('image_id',id);
        _this.delay(4000).LoopedGallery(images);
            
    });
}
